package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.ExpressCompany;
import com.burgeon.backend.EnterpriseWeChat.entity.YCOrder;
import com.burgeon.backend.EnterpriseWeChat.entity.YCOrderItem;
import com.burgeon.backend.EnterpriseWeChat.rest.Express.getExpressCompany;
import com.burgeon.backend.EnterpriseWeChat.rest.FindYcOrderItemByDocno;
import com.burgeon.backend.EnterpriseWeChat.rest.action.generateSendOrder;
import com.burgeon.backend.EnterpriseWeChat.rest.retailOut.GetOrderByDocNo;
import com.burgeon.backend.EnterpriseWeChat.rest.retailOut.GetRetailOutByOrderId;
import com.burgeon.backend.EnterpriseWeChat.rest.retailOut.sendOrder;
import com.burgeon.backend.EnterpriseWeChat.service.EWOrderService;
import com.burgeon.backend.EnterpriseWeChat.service.ex.UploadException;
import com.burgeon.backend.EnterpriseWeChat.utils.TestUrlUtils;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import com.burgeon.backend.SHOPBACKEND.entity.FOrder;
import com.burgeon.backend.SHOPBACKEND.rest.QuerryEmployee;
import com.burgeon.backend.SHOPBACKEND.rest.QuerryYCRByExr;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EWOrderServiceImpl implements EWOrderService {
    private final static Log logger = LogFactory.getLog(EWOrderService.class);
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 订单详情
     * @param token
     * @param ycOrderId
     * @return
     */
    @Override
    public YCOrder GetRetailOutByOrderId(String token, String ycOrderId) {
        Info info = GetInfoByToken(token);
        GetRetailOutByOrderId getRetailOutByOrderId = new GetRetailOutByOrderId(info.getUrl(),info.getAccount(),
                info.getPassword());
        FindYcOrderItemByDocno findYcOrderItemByDocno = new FindYcOrderItemByDocno(info.getUrl(),info.getAccount(),
                info.getPassword());
//        YCOrder ycOrder = new YCOrder();
        try {
            YCOrder ycOrder = getRetailOutByOrderId.getData(ycOrderId);
            if(ycOrder != null){
                ArrayList<YCOrderItem> ycOrderItems = new ArrayList<>();
                ArrayList<YCOrderItem> ycOrderItem = findYcOrderItemByDocno.getData(ycOrder.getDocNo());

                for (YCOrderItem ycOrderItem1:ycOrderItem) {
                    ycOrderItem1.setType(token);
                    String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".jpg";
                    String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".png";
                    if(TestUrlUtils.testUrl(jpgUrl)){
                        System.err.println(jpgUrl);
                        ycOrderItem1.setProductUrl(jpgUrl);
                    }else if (TestUrlUtils.testUrl(pngUrl)){
                        System.err.println(pngUrl);
                        ycOrderItem1.setProductUrl(pngUrl);
                    }else {
                        ycOrderItem1.setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                    }
                    ycOrderItems.add(ycOrderItem1);
                }
                ycOrder.setYcOrderItem(ycOrderItems);
                return ycOrder;
            }



        } catch (Exception e) {
            e.printStackTrace();
            throw new UploadException("订单详情"+e.getMessage());
        }
        return null;
    }

    @Override
    public ArrayList<ExpressCompany> getExpressCompany(String token) {
        Info info = GetInfoByToken(token);
        getExpressCompany getExpressCompany = new getExpressCompany(info.getUrl(),info.getAccount(),
                info.getPassword());
        ArrayList<ExpressCompany> expressCompanies = new ArrayList<>();

        try {
            expressCompanies = getExpressCompany.getData();
        } catch (Exception e) {
            e.printStackTrace();
            throw new UploadException("物流公司"+e.getMessage());
        }
        return expressCompanies;
    }

    /**
     * 发货
     * @param token
     * @param empName
     * @param expressName
     * @param expressNo
     * @param ycOrderId
     */
    @Override
    public String sendOrder(String token, String empName, String expressName, String expressNo, String ycOrderId) {
        Info info = GetInfoByToken(token);
        //写入发货
        sendOrder sendOrder = new sendOrder(info.getUrl(),info.getAccount(), info.getPassword());
        //确认发货定义
        generateSendOrder generateSendOrder = new generateSendOrder(info.getUrl(),info.getAccount(), info.getPassword());
        try {
            sendOrder.getData(empName, expressName, expressNo, ycOrderId);
            try {
                generateSendOrder.getData("YC_RETAILOUT_OK",ycOrderId);
                return "OK";
            } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new UploadException("发送失败"+e.getMessage());
        }

        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new UploadException("修改发货信息失败"+e.getMessage());
        }
    }

    /**
     * 搜索
     * @param token
     * @param docNo
     * @param storeId
     * @return
     */
    @Override
    public  ArrayList<YCOrder> FindOrderByDocNo(String token, String docNo, String storeId,String status) {
        Info info = GetInfoByToken(token);
        GetOrderByDocNo getOrderByDocNo = new GetOrderByDocNo(info.getUrl(),info.getAccount(), info.getPassword());
        FindYcOrderItemByDocno findYcOrderItemByDocno = new FindYcOrderItemByDocno(info.getUrl(),info.getAccount(),
                info.getPassword());
//        YCOrder ycOrder = new YCOrder();
        ArrayList<YCOrder> ycOrderList = new ArrayList<>();
        try {
            ArrayList<YCOrder> ycOrders = getOrderByDocNo.getData(docNo, storeId,status);
            if(ycOrders.size()>0){
                for (YCOrder ycOrder:ycOrders) {

                    ArrayList<YCOrderItem> ycOrderItems = new ArrayList<>();
                    ArrayList<YCOrderItem> ycOrderItem = findYcOrderItemByDocno.getData(ycOrder.getDocNo());

                    for (YCOrderItem ycOrderItem1:ycOrderItem) {
                        String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".jpg";
                        String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".png";
                        if(TestUrlUtils.testUrl(jpgUrl)){
                            System.err.println(jpgUrl);
                            ycOrderItem1.setProductUrl(jpgUrl);
                        }else if (TestUrlUtils.testUrl(pngUrl)){
                            System.err.println(pngUrl);
                            ycOrderItem1.setProductUrl(pngUrl);
                        }else {
                            ycOrderItem1.setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                        }
                        ycOrderItems.add(ycOrderItem1);
                    }

                    ycOrder.setYcOrderItem(ycOrderItems);
                    ycOrderList.add(ycOrder);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new UploadException("搜索错误"+e.getMessage());
        }
        return ycOrderList;
    }

    @Override
    public void FindNewOrder() {
        List<Info> accList = accountMapper.getShopAccountAll();

        for (Info info : accList) {
            QuerryYCRByExr querryYCRetailout = new QuerryYCRByExr(info.getUrl(), info.getAccount(), info.getPassword());

            try {
                //查询云仓发货单，状态为1，没有快递信息
                ArrayList<FOrder> fOrders = querryYCRetailout.getData();
                for (FOrder fOrder : fOrders) {

                    //根据发货店仓查询员工
                    QuerryEmployee querryEmployee = new QuerryEmployee(info.getUrl(), info.getAccount(), info.getPassword());
                    ArrayList<Employee> employees = querryEmployee.getData(fOrder.getcStoreId());//fOrder.getcStoreId()"石家庄长安万达店"
                    if ("Y".equals(info.getIs_app())) {
                        //推送的参数号列
                        List<String> aliasList = new ArrayList<>();
                         for (Employee employee : employees) {
                            if (!"null".equals(employee.getHandset())) {
                                aliasList.add(employee.getHandset());
                            }
                        }
                    }
                }

            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }
}
